<?php

namespace Biz\model\package;

use Doraemon\pockets\datebase\BaseDbModel;

class PackageOrder extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db';
    const _CONFIG_SELECT_ = 'db_activity';
    const _DATABASE_ = 'hhz_activity';
    const _TABLE_NAME = 'new_user_package_order';

    // 状态 1、已付款, 2、已发货, 3、已签收, 4、物流异常, 5、订单关闭, 6、部分发货, 7、支付失败，9、删除
    const STATUS_UNPAY = 0;
    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;
    const STATUS_EXPRESS = 2;
    const STATUS_RECEIVED = 3;
    const STATUS_LOGISTICS_FAIL = 4;
    const STATUS_OFF = 5;
    const STATUS_VIRTUAL = 6;
    const STATUS_FAIL = 7;  // 支付失败

    const STATUS_MAP = [
        self::STATUS_NORMAL => '待发货',
        self::STATUS_EXPRESS => '已发货',
        self::STATUS_RECEIVED => '已签收',
        self::STATUS_LOGISTICS_FAIL => '物流异常',
        self::STATUS_OFF => '订单关闭',
        self::STATUS_VIRTUAL => '部分发货',
        self::STATUS_FAIL => '支付失败',
    ];
    const PACKAGE_ID_1 = 1; //基础包
    const PACKAGE_ID_2 = 2; //高级包
    const PACKAGE_ID_3 = 3; //高级包
    const PACKAGES = [
        self::PACKAGE_ID_1 => '基础包',
        self::PACKAGE_ID_2 => '升级包',
        self::PACKAGE_ID_3 => '分析助手',
    ];

    const PACKAGES_PRICE = [
        self::PACKAGE_ID_1 => '69',
        self::PACKAGE_ID_2 => '149',
        self::PACKAGE_ID_3 => '0',
    ];

    const IS_BAD_1 = 1; //是
    const IS_BAD_2 = 2; //否
    const IS_BAD = [
        self::IS_BAD_1 => '是',
        self::IS_BAD_2 => '否',
    ];

    const STATUS_VALID = [self::STATUS_NORMAL, self::STATUS_EXPRESS, self::STATUS_RECEIVED, self::STATUS_LOGISTICS_FAIL, self::STATUS_VIRTUAL];

    public static function selectByWhere($where, $select = '*')
    {
        $list = self::select($select, $where);
        return $list;
    }

    public static function getList($where, $limit, $field='*')
    {
        if (!empty($limit)) {
            $where['LIMIT'] = $limit;
        }
        $where["status[!]"] = self::STATUS_DELETE;
        return self::select($field, $where) ? : [];
    }

    public static function countNum($where)
    {
        $countNum = self::count($where);
        return empty($countNum) ? 0 : $countNum;
    }

    public static function getByWhere($where)
    {
        return self::get('*', $where);
    }

    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = time();
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = time();
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return self::id();
    }

    public static function updateData($data, $where)
    {
        $data['edittime'] = time();
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    public static function editById($id, $data)
    {
        $data['edittime'] = time();
        if (self::update($data, ['id' => $id])) {
            return $id;
        }
        return false;
    }

    public static function getById($id , $field = "*")
    {
        return self::get($field, ['id' => $id]);
    }

}

